<?php
	class ArchivesAction extends CommonAction{
		
		function _filter(&$map){
			$map['type'] = 'article';
		}
		
		public function _before_index(){
			$this->getCategoryOption('article');
		}
		
		public function insert(){
			$model = D ( 'Archives' );
			if (false === $model->create ()) {
				$this->error ( $model->getError () );
			}
			//保存当前数据对象
			$list = $model->add ();
			if ($list!==false) { //保存成功
				D('Category')->where("id='".$_POST['cid']."'")->setInc('count');
				$this->saveTag($_POST['tags'],$list); //处理tags
				$this->checkSession();
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				$this->success ('新增成功!');
			} else {
				//失败提示
				$this->error ('新增失败!');
			}
		}
		
		public function update(){
			$model = D ( 'Archives' );
			if (false === $model->create ()) {
				$this->error ( $model->getError () );
			}
			// 更新数据
			$list = $model->save ();
			if (false !== $list) {
				$this->saveTag($_POST['tags'],$_POST['id']);  //处理tags
				$this->checkSession();
				//成功提示
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				$this->success ('编辑成功!');
			} else {
				//错误提示
				$this->error ('编辑失败!');
			}
		}
		
		public function _before_update(){//编辑文档检查是否有上传缩略图，如果有，则删除原缩略图。
			if ( isset( $_SESSION["crop_zoom"] ) ){
				$new_file = $_SESSION["crop_zoom"]['foder'] . '/thumb_' . $_SESSION["crop_zoom"]['name'];
				if ( $_POST['o_thumb'] != $new_file ){
					@unlink( './' . C('WEB_PUBLIC_PATH') . '/' . C('DIR_ATTCH_PATH') . '/' . $_POST['o_thumb'] );
				}
			}
		}
		
		/**
		 * ===================================================
		 * 删除文档
		 * 
		 * ===================================================
		 */
		public function delete(){
			if ( isset( $_GET['id'] ) ){
				$this->eDelete( 'archives' , $_GET['id'] );
			}
		}
		
		public function _before_add()
		{
			$this->getCategoryOption('article');
		}
		
		public function _before_edit(){
			$this->getCategoryOption('article', $_GET['cid']);
		}
		
		public function moveCatelog(){
			$model = D('Archives');
			if ( $_POST['movecatelog'] ){
				if ( count( $_POST['ids'] ) > 0 ){
					$where['id'] = array( 'in', $_POST['ids'] );
					if ( $model->where( $where )->setField('cid',$_POST['cid']) ){
						$this->success( '转移成功' );
					}else{
						$this->error( '转移失败' );
					}
				}
			}
		}
	}
